<?php

$smarty->configLoad($lang . '/rule.module.php');
$language = parse_ini_file($lang . '/breadcrumb.module.php', true);
$breadcrumb = $language['rule_detail'];
$template = 'rules/detail.tpl';
$sql = 'SELECT * FROM ' . DB_CMS . '.module_rules WHERE id = :id';
$dbh = $connection->prepare($sql);
$dbh->bindValue(':id', $_GET['id']);
$dbh->execute();
$row = $dbh->fetch(PDO::FETCH_ASSOC);
$smarty->assign('title', $row['name']);
$smarty->assign('id', $row['id']);


$sql = 'SELECT * FROM ' . DB_CMS . '.module_rules_points WHERE ruleID = :ruleID';
$qry = $connection->prepare($sql);
$qry->bindValue(':ruleID', $row['id']);
$qry->execute();
while ($row = $qry->fetch(PDO::FETCH_ASSOC)) {
	$value[] = $row;
}
$smarty->assign('from', $value);

if (isset($_POST['edit_protocol'])) {
	$main->logAction($_SESSION['id'], 'rules_edit_log');
	$_GET['id'] = $_POST['ruleID'];
	$sql = 'UPDATE '.DB_CMS.'.module_rules SET name = :name WHERE id = :id';
	$stm = $connection->prepare($sql);
	$stm->bindValue(':name', $_POST['protocol_name']);
	$stm->bindValue(':id', $_GET['id']);
	$stm->execute();

	$i = 0;
	$sql = 'DELETE FROM '.DB_CMS.'.module_rules_points WHERE ruleID = :ruleID';
	$qry2 = $connection->prepare($sql);
	$qry2->bindValue(':ruleID', $_GET['id']);
	$qry2->execute();
	foreach ($_POST['protocol_titel'] as $value) {
		if (!empty($_POST['protocol_titel'][$i])) {
			$sql = 'INSERT INTO '.DB_CMS.'.module_rules_points (ruleID, text, title) VALUES (:ruleID, :text, :title)';
			$qry1 = $connection->prepare($sql);
			$qry1->bindValue(':ruleID', $_GET['id']);
			$qry1->bindValue(':text', $_POST['protocol_information'][$i]);
			$qry1->bindValue(':title', $_POST['protocol_titel'][$i]);
			$qry1->execute();
			$i++;
		}
	}
	$main->jqmodal('edited');
	echo '<meta http-equiv="refresh" content="5; URL=index.php?mod=rules&page=detail&id='.$_GET['id'].'">';
}
if(empty($_GET['action'])) {
    $_GET['action'] = '';
}
if ($_GET['action'] === 'delete') {
	$main->logAction($_SESSION['id'], 'rules_del_log');
	$sql = 'DELETE FROM '.DB_CMS.'.module_rules WHERE id = :id';
	$stm = $connection->prepare($sql);
	$stm->bindValue(':id', $_GET['id']);
	$stm->execute();
	$sql = 'DELETE FROM '.DB_CMS.'.module_rules_points WHERE ruleID = :id';
	$dbh = $connection->prepare($sql);
	$dbh->bindValue(':id', $_GET['id']);
	$dbh->execute();
	$main->jqmodal('deletet');
	echo '<meta http-equiv="refresh" content="5; URL= index.php?mod=rules">';
}
?>
